/** @jsxImportSource @emotion/react */
import * as React from "react";
import {
    Text, Table, TableBody, TableRow, Cell, CCell, Button, Collapse, TableHead, IconCheckSquare, IconSquare,
} from "customize-easy-ui-component";
import { DirectLink } from "../../../routing/Link";
import {ReportViewProps, ReportViewSubProps,} from "../../common/base";
import {分项末尾链接,} from "../../common/rarelyVary";
import {useMainRepUrlOr, useSplitSubCapacity, useSplitSubRepList} from "../../hook/useMainRepUrlOr";
import {useContext} from "react";
import RoutingContext from "../../../routing/RoutingContext";
import {config测量, item校核依据,} from "./Detection.O-1";
import {TailMenRowIspCheck} from "../../contain/rarelyVary";

/**@param fxIdx: 独立流转模式的 分项序号的基数； 若是嵌入式的分项该参数=undefined! rep?.id!==repId
 * 只要是公共的模板：那modelConfigs.ts就不能少了配置该模板代码。
 * */
export const ReportView: React.FunctionComponent<ReportViewProps> = (
    {repId,   source: parentOrc,  verId,rep, redId,fxIdx ,...other}
) => {
    const {urlMainRep}=useMainRepUrlOr(rep);
    const lsBlockMax=useSplitSubCapacity(parentOrc?._STRENGTH_VFB?.length,1);       //【模板敲定一次性最大几个分项显示出来】有些分项报告模板可能存在很多个分项 最多1个
    const {sumArea, areaIdAs, btnBindUses}=useSplitSubRepList(parentOrc?._STRENGTH_VFB ?? [], lsBlockMax,false);
    const { get:getRouteUrl } = useContext(RoutingContext);
    const routeLevels=getRouteUrl().entries.length;
    const routeTemplate=getRouteUrl().entries[routeLevels-1]?.routeData?.params?.template;
    return (
        <React.Fragment>
          <div  id="rp_strength_vfb_"  css={{"@media not print": {marginTop:'1rem', marginBottom: '1rem'},}}>
             <DirectLink  href={`/report/STRENGTH_VFB/ver/${verId}/${repId}/STRENGTH_VFB/_Controller`}>
                <div css={{"@media print":{display: 'none'} }}>
                    <Button>{(repId===rep?.id && rep?.modeltype!=='STRENGTH_VFB')? '内嵌式的':'独立流转的'}强度校核报告分项控制器</Button>
                </div>
             </DirectLink>
            {(new Array(sumArea).fill(null)).map((s:any,ak:number) => {
                const [isDisplay, bindBtn]=btnBindUses[ak];      //不要加上 tabIndex={0} 超过折叠区域最大数量的？
                return <React.Fragment key={ak}>
                    <div key={ak} role="button" {...bindBtn}  css={{"@media print":{display: 'none'}}}>
                        { <Text variant="h4">{`强度校核折叠区${ak+1}，`}{isDisplay ? `收起`:`更多..`}</Text> }
                    </div>
                    <Collapse id={`${ak}`} show={isDisplay} noAnimated>
                        { areaIdAs[ak]?.map((surpId: any, m:number) => {
                            const dispIdx=(fxIdx??0) +(m+ ak*lsBlockMax);
                            return <OneSubRepView key={m} fxIdx={fxIdx} dispIdx={dispIdx} rep={rep} repId={repId}
                                                  verId={verId} parentOrc={parentOrc} surpId={surpId}/>
                        }) }
                        <div role="button" {...bindBtn}  css={{"@media print":{display: 'none'}}}>
                            <Text variant="h4">{`强度校核折叠区${ak+1}结束,收起`}</Text>
                        </div>
                    </Collapse>
                </React.Fragment>
            })}
          </div>
          {分项末尾链接({template:'STRENGTH_VFB', repId: repId||'',verId,urlMainRep,
                nestIn:'STRENGTH_VFB'!==routeTemplate, flowNest: repId!==rep?.id}) }
       </React.Fragment>
    );
}

//【表格】 【助手】 可重复分项情况 JSON.parse(parentOrc?._tblFixed??'[]')  【工具】 非主报告底下的情形。
/**@param fxIdx: 独立流转模式的 分项序号的基数； 若是嵌入式的分项该参数=undefined! rep?.id!==repId
 * */
const OneSubRepView: React.FunctionComponent<ReportViewSubProps> = (
    {fxIdx,dispIdx,rep,repId,verId,parentOrc,surpId}
) => {
    const orc=parentOrc['_STRENGTH_VFB_'+surpId];
    // const [renderHead,render测量]=useContentTableViewer(config测量, orc?.测量表,true,true);
    return <React.Fragment>
        <div css={{"@media print": {paddingBottom: '3.5rem', pageBreakInside: 'avoid'}}}>
            <Text variant="h2" css={{
                textAlign: 'center', marginTop: '1rem',
            }}>强度校核报告{dispIdx ? '-' + dispIdx : ''}</Text>
            <div css={{
                display: 'flex',
                justifyContent: 'space-between'
            }}>
                <Text></Text>
                <Text>报告编号：{rep.isp.no}</Text>
            </div>
        </div>
        <Table fixed={ ["13%","%"] } tight miniw={800}
                  css={{borderCollapse: 'collapse', "@media print": {marginTop: '-3.5rem'}}}>
            <TableBody>
                <DirectLink href={`/report/STRENGTH_VFB/ver/${verId}/${repId}/${surpId}/PartSummary`}>
                    <TableRow>
                        <CCell>部件名称</CCell><CCell>{orc?.部件??'／'}</CCell>
                    </TableRow>
                    {item校核依据.map(([name,desc]: any, i: number) => {
                        return <TableRow key={i}>
                            {(0===i) && <CCell rowSpan={item校核依据.length}>校核依据</CCell> }
                            <CCell>
                                <div key={i} css={{display: 'flex', alignItems: 'center'}}>{orc?.[name] ?
                                    <IconCheckSquare size='sm'/> : <IconSquare size='sm'/>}&nbsp;{desc}
                                </div>
                            </CCell>
                        </TableRow>
                    })}
                </DirectLink>
            </TableBody>
        </Table>
        <Table id='EvaluationForm' fixed={ ["3.5%","19%","7%","10%","%","13%"] } tight miniw={800} css={{borderCollapse: 'collapse', }}>
            <TableHead>
              <DirectLink href={`/report/STRENGTH_VFB/ver/${verId}/${repId}/${surpId}/EvaluationForm`}>
                  <TableRow><CCell><Text css={{fontSize: '0.75rem'}}>序号</Text></CCell>
                      {config测量.map(([title,_2,_1], i:number) => {
                          return <CCell key={i}>{title}</CCell>;
                      }) }
                  </TableRow>
              </DirectLink>
            </TableHead>
            <TableBody>
                <DirectLink href={`/report/STRENGTH_VFB/ver/${verId}/${repId}/${surpId}/EvaluationForm`}>
                    {!(orc?.测量表?.length>0) ?
                        <TableRow><CCell >/</CCell>
                            {config测量.map(([title,aName,_1], c:number) => {
                                return <CCell key={c}>{'／'}</CCell>;
                            }) }
                        </TableRow>
                        :
                        <>
                            {orc?.测量表?.map((o: any, i:number) => {
                                return (<TableRow key={i}><CCell >{i+1}</CCell>
                                        {config测量.map(([title,aName,_1], c:number) => {
                                            return c===3? <Cell key={c}><div css={{whiteSpace: 'pre-wrap'}}>
                                                            {o?.[aName]??((o)&&'／')}
                                                        </div></Cell>
                                                        :
                                                <CCell key={c}>{o?.[aName]??((o)&&'／')}</CCell>;
                                        }) }
                                    </TableRow>
                                );
                            }) }
                        </>
                    }
                </DirectLink>
            </TableBody>
        </Table>
        <TailMenRowIspCheck orc={orc} rep={rep} href={`/report/STRENGTH_VFB/ver/${verId}/${repId}/${surpId}/PartSummary`}>
            <DirectLink href={`/report/STRENGTH_VFB/ver/${verId}/${repId}/${surpId}/PartSummary`}>
                <TableRow>
                    <Cell split={true} colSpan={2}><div css={{minHeight: '2rem', whiteSpace: 'pre-wrap'}}>
                        校核结论：<br/>
                        {orc?.检测结果 || '／'}
                    </div></Cell>
                </TableRow>
                <TableRow>
                    <Cell split={true} colSpan={2}>
                        <Text css={{fontSize:'0.8rem'}}>
                            备注：1、此次校核计算仅对本报告有效，不作为其他任何检验依据。<br/>
                            <Text css={{textIndent:'2.4rem',display:'block',fontSize:'0.8rem'}}>2、本校核不代替设计计算，不能免除设计者责任。</Text>
                        </Text>
                    </Cell>
                </TableRow>
            </DirectLink>
        </TailMenRowIspCheck>
    </React.Fragment>;
}
